package com.qyhl.school.school.reporter.list;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.school.SchoolReporterActBean;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

@Route(path = ARouterPathConstant.SCHOOL_REPORTER_ACT_LIST)
public class SchoolReporterActListActivity extends BaseActivity implements SchoolReporterActListContract.SchoolReporterActListView {
    @BindView(R2.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.title)
    TextView title;

    private SchoolReporterActListPresenter mPresenter;

    private int lastId;

    private CommonAdapter<SchoolReporterActBean> commonAdapter;

    private List<SchoolReporterActBean> mDataList = new ArrayList<>();

    @Override
    protected int getContentView() {
        return R.layout.activity_school_reporter_act_list;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true).fitsSystemWindows(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new SchoolReporterActListPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);

        String titleName = getIntent().getStringExtra("title");

        if (StringUtils.isNotEmpty(titleName)) {
            title.setText(titleName);
        } else {
            title.setText("活动");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(commonAdapter = new CommonAdapter<SchoolReporterActBean>(this, R.layout.item_school_reporter_act_home, mDataList) {
            @Override
            protected void convert(ViewHolder holder, SchoolReporterActBean bean, int i) {
                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(SchoolReporterActListActivity.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.cover_large_default).placeholder(R.drawable.cover_large_default))
                        .into(cover);

                holder.setText(R.id.title, bean.getTitle());

                holder.setText(R.id.address, bean.getAddress());

                holder.setText(R.id.num, bean.getReportCount() + "个小伙伴");

                if (bean.isReportOver()) {
                    holder.setImageResource(R.id.tag, R.drawable.school_new_home_reporter_end_tag);
                } else if (bean.isReportNotStart()) {
                    holder.setImageResource(R.id.tag, R.drawable.school_new_home_reporter_forecast_tag);
                } else {
                    holder.setImageResource(R.id.tag, R.drawable.school_new_home_reporter_tag);
                }
                holder.setVisible(R.id.sign_btn, false);

                if (StringUtils.isNotEmpty(bean.getEndDate())) {
                    holder.setText(R.id.date, bean.getStartDate() + "—" + bean.getEndDate());
                } else {
                    holder.setText(R.id.date, bean.getStartDate());
                }

                holder.setOnClickListener(R.id.sign_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                            @Override
                            public void login(boolean isLogin) {
                                if (isLogin) {
                                    showLoading();
                                    String username = CommonUtils.getInstance().getUserName();
                                    mPresenter.doSign(username, bean.getId());
                                } else {
                                    Toasty.normal(SchoolReporterActListActivity.this, "尚未登录或登录已失效！").show();
                                    RouterManager.routerLogin(SchoolReporterActListActivity.this, 0);
                                }
                            }

                            @Override
                            public void error(String s) {

                            }
                        });
                    }
                });
            }
        });

        mPresenter.getList(lastId);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                lastId = 0;
                mPresenter.getList(lastId);
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                lastId = 0;
                mPresenter.getList(lastId);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(lastId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                ARouter.getInstance().build(ARouterPathConstant.SCHOOL_REPORTER_ACT_DETAIL)
                        .withInt("actId", mDataList.get(position).getId())
                        .navigation();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void setList(List<SchoolReporterActBean> beanList, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        lastId = beanList.get(beanList.size() - 1).getId();
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(this)) {
                showToast("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                showToast("暂无更多小记者团活动！");
            } else {
                showToast(msg);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setReloadButtonText("点击重试~");
            if (!NetUtil.isNetworkAvalible(this)) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }

    @Override
    public void signResult(boolean isSuccess, String msg) {
        dismissLoading();
        showToast(msg);
    }

    @OnClick(R2.id.back_btn)
    public void onClick() {
        finish();
    }

}
